package com.onec.service.channel.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.onec.service.api.content.AesEntityContent;
import com.onec.service.api.enums.ProductEnum;
import com.onec.service.api.enums.StatusEnum;
import lombok.Data;
import org.hibernate.annotations.ColumnTransformer;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 通道订单明细
 *
 * @author ONEC
 */
@Data
@Entity
@Table(name = "t_channel_order_detail")
public class ChannelOrderDetail implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true)
    private Long id;

    @Column(name = "version", length = 8)
    private Integer version;

    /**
     * 订单id
     */
    @Column(name = "online_order_id", length = 11)
    private Long onlineOrderId;
    /**
     * 商户编号
     */
    @Column(name = "merchant_no", length = 32)
    private String merchantNo;
    /**
     * 商户编号
     */
    @Column(name = "channel_merchant_no", length = 32)
    private String channelMerchantNo;
    /**
     * 商户名称
     */
    @Column(name = "merchant_name", length = 32)
    private String merchantName;

    /**
     * 通道编号
     */
    @Column(name = "channel_name", length = 32)
    private Long channelName;
    /**
     * 通道编号
     */
    @Column(name = "channel_no", length = 32)
    private String channelNo;

    /**
     * 类型码编号
     */
    @Column(name = "business_no", length = 32)
    private String businessNo;
    /**
     * 类型码编号
     */
    @Column(name = "channel_business_no", length = 32)
    private String channelBusinessNo;

    /**
     * 下游订单号
     */
    @Column(name = "external_num", length = 32, unique = true)
    private String externalNum;

    /**
     * 平台流水号
     */
    @Column(name = "serial_num", length = 32, unique = true)
    private String serialNum;

    /**
     * 平台流水号
     */
    @Column(name = "trade_num", length = 32, unique = true)
    private String tradeNum;
    /**
     * 商户手续费
     */
    @Column(name = "merchant_rate", length = 8, scale = 4)
    private BigDecimal merchantRate;
    /**
     * 商户手续费
     */
    @Column(name = "merchant_fee", length = 8, scale = 1)
    private BigDecimal merchantFee;

    /**
     * 通道手续费（包含固定值）
     */
    @Column(name = "channel_rate", length = 8, scale = 4)
    private BigDecimal channelRate;
    /**
     * 通道手续费（包含固定值）
     */
    @Column(name = "channel_fee", length = 8, scale = 1)
    private BigDecimal channelFee;


    /**
     * 交易金额
     */
    @Column(name = "amount", length = 8, scale = 2)
    private BigDecimal amount;

    /**
     * 优惠金额
     */
    @Column(name = "coupon_amount", length = 8, scale = 2)
    private BigDecimal couponAmount;

    /**
     * 优惠信息：jsonobject
     */
    @Column(name = "product", length = 8)
    @Enumerated(EnumType.STRING)
    private ProductEnum product;
    /**
     * 创建时间
     */
    @Column(name = "create_time", length = 0)
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date createTime;

    /**
     * 完成时间
     */
    @Column(name = "complete_time", length = 0)
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date completeTime;

    /**
     * 通道完成时间
     */
    @Column(name = "channel_complete_time", length = 0)
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date channelCompleteTime;
    /**
     * 通道完成时间
     */
    @Column(name = "update_time", length = 0)
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:ss:mm", timezone = "GTM+8")
    private Date updateTime;

    /**
     * 交易订单状态
     */
    @Column(name = "status", length = 12)
    @Enumerated(EnumType.STRING)
    private StatusEnum status;

    /**
     * 订单出款状态
     */
    @Column(name = "withdrew_status", length = 12)
    @Enumerated(EnumType.STRING)
    private StatusEnum withDrewStatus;

    /**
     * 交易卡号
     */
    @Column(name = "card_no")
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_CARD_NO,
            write = AesEntityContent.AES_ENCRYPT_CARD_NO
    )
    private String cardNo;
    /**
     * 交易卡号
     */
    @Column(name = "id_card")
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_ID_CARD,
            write = AesEntityContent.AES_ENCRYPT_ID_CARD
    )
    private String idCard;
    /**
     * 交易卡号
     */
    @Column(name = "name")
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_NAME,
            write = AesEntityContent.AES_ENCRYPT_NAME
    )
    private String name;

    /**
     * 银行名称
     */
    @Column(name = "bank_name", length = 32)
    private String bankName;

    /**
     * 交易手机号
     */
    @ColumnTransformer(
            read = AesEntityContent.AES_DECRYPT_PHONE,
            write = AesEntityContent.AES_ENCRYPT_PHONE
    )
    @Column(name = "phone")
    private String phone;


    /**
     * 产品备注
     */
    @Column(name = "remarks", length = 64)
    private String remarks;

    /**
     * 产品备注
     */
    @Column(name = "channel_merchant_name", length = 64)
    private String channelMerchantName;
    /**
     * 产品备注
     */
    @Column(name = "mcc", length = 8)
    private String mcc;
    /**
     * 产品备注
     */
    @Column(name = "city", length = 8)
    private String city;
    /**
     * 产品备注
     */
    @Column(name = "return_type", length = 24)
    private String returnType;
    /**
     * 产品备注
     */
    @Column(name = "url")
    private String url;


    public ChannelOrderDetail(ChannelInfo channelInfo) {

        this.version = 1;
        this.onlineOrderId = onlineOrderId;
        this.merchantNo = merchantNo;
        this.merchantName = merchantName;
        this.channelName = channelName;
        this.businessNo = businessNo;
        this.externalNum = externalNum;
        this.serialNum = serialNum;
        this.tradeNum = tradeNum;

        this.merchantRate = merchantRate;
        this.merchantFee = merchantFee;
        this.channelRate = channelRate;
        this.channelFee = channelFee;
        this.amount = amount;
        this.couponAmount = couponAmount;
        this.product = product;
        this.createTime = createTime;
        this.completeTime = completeTime;
        this.channelCompleteTime = channelCompleteTime;
        this.updateTime = updateTime;
        this.status = status;
        this.withDrewStatus = withDrewStatus;
        this.cardNo = cardNo;
        this.bankName = bankName;
        this.phone = phone;
        this.remarks = remarks;
        this.channelMerchantName = channelMerchantName;
        this.mcc = mcc;
        this.city = city;
        this.returnType = returnType;
        this.url = url;
    }
}
